{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "annoying-sunset",
   "metadata": {},
   "source": [
    "# Interact with a WMS¶\n",
    "\n",
    "OGC Web Map Service (WMS) can be used to download map images rendered by the remote server.\n",
    "\n",
    "Example from [GeoPython Workshop](https://github.com/geopython/geopython-workshop/blob/master/workshop/jupyter/content/notebooks/10-remote-data.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "expired-excess",
   "metadata": {},
   "source": [
    "**This is the metadata of the service endpoint:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sustainable-sellers",
   "metadata": {},
   "outputs": [],
   "source": [
    "from owslib.wms import WebMapService\n",
    "\n",
    "wms_url = \"https://ows.terrestris.de/osm/service\"\n",
    "\n",
    "wms = WebMapService(wms_url, version=\"1.3.0\")\n",
    "\n",
    "print(f\"WMS version: {wms.identification.version}\")\n",
    "print(f\"WMS title: {wms.identification.title}\")\n",
    "print(f\"WMS abstract: {wms.identification.abstract}\")\n",
    "print(f\"Provider name: {wms.provider.name}\")\n",
    "print(f\"Provider address: {wms.provider.contact.address}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "directed-carrier",
   "metadata": {},
   "source": [
    "Check the [Capabilities](https://ows.terrestris.de/osm/service?service=WMS&request=GetCapabilities&version=1.3.0) response directly from the server"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "assigned-fundamental",
   "metadata": {},
   "source": [
    "**Available WMS layers:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "elder-latvia",
   "metadata": {},
   "outputs": [],
   "source": [
    "list(wms.contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "standard-vehicle",
   "metadata": {},
   "outputs": [],
   "source": [
    "wms.contents['SRTM30-Colored'].boundingBox"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "geological-netscape",
   "metadata": {},
   "outputs": [],
   "source": [
    "wms.contents['SRTM30-Colored'].boundingBoxWGS84"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "figured-technician",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(wms['SRTM30-Colored'].crsOptions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "champion-toddler",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(wms['SRTM30-Colored'].styles)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "discrete-prescription",
   "metadata": {},
   "source": [
    "**Available methods, their URLs, and available formats:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "disabled-engineer",
   "metadata": {},
   "outputs": [],
   "source": [
    "[op.name for op in wms.operations]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "handled-nightlife",
   "metadata": {},
   "outputs": [],
   "source": [
    "wms.getOperationByName('GetMap').methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "after-colon",
   "metadata": {},
   "outputs": [],
   "source": [
    "wms.getOperationByName('GetMap').formatOptions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "protective-indonesian",
   "metadata": {},
   "source": [
    "**That's everything needed to make a request for imagery:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "emerging-paint",
   "metadata": {},
   "outputs": [],
   "source": [
    "img = wms.getmap(\n",
    "    layers=['SRTM30-Colored'],\n",
    "    size=[600, 400],\n",
    "    srs=\"EPSG:4326\",\n",
    "    bbox=[1.0, 50.0, 10.0, 54.0],\n",
    "    format=\"image/jpeg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "integral-thong",
   "metadata": {},
   "source": [
    "**Result:**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "organic-pennsylvania",
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "Image(img.read())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
